package com.ymatou.iapi.hunter.testcase;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.hunter.paramater.GetHunterAncestryRequest;
import com.ymatou.iapi.hunter.paramater.bean.HunterAncestryBean;
import com.ymatou.iapi.hunter.service.GetHunterAncestryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.hunter.HunterAncestry;
import com.ymttest.database.sqlwapper.hunter.HunterAncestryWrapper;

public class Ts_GetHunterAncestry {
	
	private static GetHunterAncestryRequest getHunterAncestryRequest;
	private static GetHunterAncestryCall getHunterAncestryCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查看推手所有上级");
	}

	@Before
	public void caseUp() {
		getHunterAncestryRequest = new GetHunterAncestryRequest();
		getHunterAncestryCall = new GetHunterAncestryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getHunterAncestryCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb(JSONObject object) throws JSONException {

		HunterAncestryWrapper wrapper = new HunterAncestryWrapper();
		String ancestryExpect = "";
		JSONArray ancestryList = object.getJSONArray("ancestry");
		
		for (int i = 0; i < ancestryList.size(); i++) {
			
			HunterAncestryBean bean = (HunterAncestryBean) JSONObject.toBean(
					ancestryList.getJSONObject(i), HunterAncestryBean.class);
			
			if (i == ancestryList.size() - 1) {
				ancestryExpect = ancestryExpect + bean.toString();
			} else {
				ancestryExpect = ancestryExpect + bean.toString() + ",";
			}
		}
		
		HunterAncestry hunterAncestry = wrapper.selectByHunterIdAndOrderId(
				getHunterAncestryRequest.getHunterId(),
				getHunterAncestryRequest.getOrderId());
		
		Logger.verifyEquals(ancestryExpect, hunterAncestry.getAncestry(),
				"验证ancestry");
		Logger.verifyEquals(object.getInt("hunterLevel"), hunterAncestry.getHunter_level(),
				"验证hunterLevel");
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterAncestry_001() {
		Logger.start(true, "查看推手所有上级");
		try {
			getHunterAncestryRequest.setHunterId(4321L);
			getHunterAncestryRequest.setOrderId(1380040246);
			
			getHunterAncestryCall.setData(getHunterAncestryRequest);
			getHunterAncestryCall.callService();
			
			Logger.verifyEquals("success", getHunterAncestryCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterAncestryCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterAncestryCall.getResponseData().getBoolean("success"),
					"验证success");
			
			verifyDb(getHunterAncestryCall.getResult());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterAncestry_002() {
		Logger.start(true, "hunterId为0");
		try {
			getHunterAncestryRequest.setHunterId(0);
			getHunterAncestryRequest.setOrderId(1111L);
			
			getHunterAncestryCall.setData(getHunterAncestryRequest);
			getHunterAncestryCall.callService();
			
			Logger.verifyEquals("hunterId:推手用户编号不能为空", getHunterAncestryCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterAncestryCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(false, getHunterAncestryCall.getResponseData()
					.getBoolean("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterAncestry_003() {
		Logger.start(true, "orderId为0");
		try {
			getHunterAncestryRequest.setHunterId(4321L);
			getHunterAncestryRequest.setOrderId(0);
			
			getHunterAncestryCall.setData(getHunterAncestryRequest);
			getHunterAncestryCall.callService();
			
			Logger.verifyEquals("orderId:订单编号不能为空", getHunterAncestryCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterAncestryCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(false, getHunterAncestryCall.getResponseData()
					.getBoolean("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
